<template>
	<!-- #ifdef MP-WEIXIN -->
	<uv-navbar
	  :fixed="false"
	  :title="title"
	  left-arrow
	  @leftClick="$onClickLeft"
	/>
	<!-- #endif -->
	
	<view v-if="!loading">
		<view class="nav">
			<view class="header">
				<view class="mr-1"><image :src="store.image" style="width:80rpx ; height: 80rpx; "></image></view>
				<view class="left"  style="">
					<view class="store-name" @click="selectShop()">
						<text>{{ store.name }}</text>
						<view class="iconfont iconarrow-right"></view>
					</view>
					<view class="store-location">
						<text>距离您 {{kmUnit(store.dis)}}</text>
					</view>
				</view>
				<view class="right" v-if="store.status == 1">
					<view class="dinein active" >
						<text>预约</text>
					</view>
				</view>
				<view class="right" v-else>
					<view class="takeout">
						<text>已歇业</text>
					</view>
				</view>
			</view>
		</view>
		<uv-vtabs 
				:chain="chain"
				:list="list"
		
				@change="change">
				<uv-vtabs-item>
					<view 
						class="item p-2"
						v-for="(item2,index2) in list2"
						:key="index2">
						<view class="item-content flex align-center">
							<view class="pr-2"><image :src="item2.image ? item2.image:'/static/images/mine/default.png'" mode="aspectFill" style="width: 200rpx;height: 200rpx;"></image></view>
							<view>
								<view class="font-size-medium text-color-base font-size-lg font-weight-bolder">{{item2.title}}</view>
								<view class="font-size-sm mt-1 text-color-assist">桌号:{{item2.number}}</view>
								<view class="mt-1"  @click="gopage('/pages/components/pages/due/detail?id='+item2.id)"><uv-button type="warning" shape="circle" size="mini" text="预约"></uv-button></view>
							</view>
						</view>
					</view>
				</uv-vtabs-item>
		</uv-vtabs>
	</view>
	<view class="loading" v-else>
		<uv-loading-icon  color="#DA5650" size=40 mode="circle" ></uv-loading-icon>
		<button type="primary" style="z-index: 3001;position: absolute;top: 650rpx;left:260rpx;" @click="init"
			v-if="!store.id">定位最近的门店</button>
	</view>
</template>

<script setup>
import {
  ref,
  computed
} from 'vue'
import { useMainStore } from '@/store/store'
import { storeToRefs } from 'pinia'
import { onLoad,onShow ,onPullDownRefresh,onHide} from '@dcloudio/uni-app'
import { formatDateTime,kmUnit } from '@/utils/util'
import {
  getList
} from '@/api/due'
import {
  shopNearby
} from '@/api/goods'
const main = useMainStore()
const { orderType,store,isLogin,location} = storeToRefs(main)
const title = ref('预约')
const list = ref([])
const value = ref(0)
const chain = ref(false)
const loading = ref(true) 


onLoad(() => {
	init()
})

const list2 = computed(() => { //计算购物车总数
	const _list = list.value[value.value]?.childrens
	return _list ? _list : []
})

uni.$on('refreshMenu', () => {
	// 在这里执行onLoad逻辑
	init()
})

const change = (index) =>{
	value.value = index;
}


const  init = async() => { //页面初始化
	loading.value = true;
	let error = {},
		result = location.value
	if (!location.value.hasOwnProperty('latitude')) {
		  uni.getLocation(({
			 type: 'wgs84',
			 success: function (res) {
				result = {
					latitude: res.latitude,
					longitude: res.longitude
				};
				getShopList(result)
			 },
			 fail: function (res) {
			   uni.showToast({
			     title: '获取位置失败，请检查是否开启相关权限',
			     duration: 2000,
			     icon: 'error'
			   });
			   // 默认地为你为北京地址
			   result = {
			   	latitude: 39.919990,
			   	longitude: 116.456270
			   };
			   getShopList(result)
			 },
			 complete: function (res) {
			 }
		}));
		
		return
	}
	
	getShopList(result)
   
	
}

const getShopList = async(res) => {
	if (res) {
		main.SET_LOCATION(res);
	
		let shop_id = 0;
		if (store.value.id) {
			shop_id = store.value.id;
		}
	
		let shop = await shopNearby({
			lat: res.latitude,
			lng: res.longitude,
			shop_id: shop_id,
			kw: ''
		});
		if (shop) {
			console.log("店铺2",main.store)
			// 设置店铺信息
			main.SET_STORE(shop);
			let desks = await getList({
				shopId: shop.id
			});
			list.value = desks
			loading.value = false;
		}
	}
}

const selectShop = () => {
	uni.navigateTo({
		url: '/pages/components/pages/shop/shop?type=2'
	})
}

const gopage = (url) =>{
	uni.navigateTo({
		url
	})
}

</script>

<style lang="scss" scoped>
	.nav {
		width: 100%;
		//height: 212rpx;
		height: 140rpx;
		display: flex;
		flex-direction: column;
	
		.header {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx;
			background-color: #ffffff;
			height: 140rpx;
	
			.left {
				flex: 1;
				display: flex;
				flex-direction: column;
	
				.store-name {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					font-size: $font-size-lg;
					margin-bottom: 10rpx;
					.small {
						font-size: $font-size-sm;
						color: $text-color-assist;
					}
					.iconfont {
						margin-left: 10rpx;
						line-height: 100%;
					}
				}
	
				.store-location {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					color: $text-color-assist;
					font-size: $font-size-sm;
	
					.iconfont {
						vertical-align: middle;
						display: table-cell;
						color: $color-primary;
						line-height: 100%;
					}
				}
			}
	
			.right {
				//background-color: $bg-color-grey;
				border-radius: 38rpx;
				display: flex;
				align-items: center;
				font-size: $font-size-sm;
				padding: 0 38rpx;
				color: $text-color-assist;
	
				.dinein,
				.takeout {
					position: relative;
					display: flex;
					align-items: center;
					&.active {
						padding: 14rpx 38rpx;
						color: #ffffff;
						background-color: $color-primary;
						//background-color: #5A5B5C;
						border-radius: 38rpx;
					}
				}
	
				.takeout {
					margin-left: 20rpx;
					height: 100%;
					flex: 1;
					padding: 14rpx 0;
				}
	
				.dinein.active {
					margin-left: -38rpx;
				}
	
				.takeout.active {
					margin-right: -38rpx;
				}
			}
		}
	

	}
	
</style>